using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YDExtendTool.Scripts;
using Object = System.Object;

public interface IGetValue
{
    object GetValue();
}

public abstract class DDACompare : DDACondition
{
    public override int NextNodeMaxNum => 2;
    public abstract bool Compare(IComparable a,IComparable b);

    public override bool CanConnect(TreeNodeBase treeNodeBase)
    {
        return base.CanConnect(treeNodeBase) && treeNodeBase is IGetValue;
    }

    public override bool Invoke()
    {
        base.Invoke();
        if (ChildNodes.Count != 2)
        {
            return true;
        }

        if (ChildNodes[0] is IGetValue a && ChildNodes[1] is IGetValue b)
        {
            var ca = a.GetValue();
            var cb = b.GetValue();
            if (ca is IComparable value1)
            {
                var value2 = Convert.ChangeType(cb, ca.GetType()) as IComparable;
                return Compare(value1,value2);
            }
        }

        return true;
    }
}

public class DDACompareGreater : DDACompare
{
    public override bool Compare(IComparable a, IComparable b)
    {
        return a.CompareTo(b) >0;
    }
}

public class DDACompareLess : DDACompare
{
    public override bool Compare(IComparable a, IComparable b)
    {
        return a.CompareTo(b) <0;
    }
}


public class DDACompareEquial : DDACompare
{
    public override bool Compare(IComparable a, IComparable b)
    {
        return a.CompareTo(b) == 0;
    }
}
